/*
 * @Author: zuimo
 * @Date: 2020-11-27 15:03:12
 * @LastEditTime: 2020-11-27 17:01:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /demo/dataStruct/src/ts/sigleLinkList.ts
 */




class SigleLinkNode {
    value: any
    next: SigleLinkNode
    constructor(element) {
        this.value = element;
        this.next = null;
    }
}

function defaultEqual(a: any, b: any): boolean {
    return a === b
}

class SigleLinkList {
    rear: SigleLinkNode
    count: number;
    equals: any
    constructor(userEqual?) {
        this.rear = null;
        this.count = 0;
        this.equals = userEqual || defaultEqual
    }

    push(element: any): void {
        // create Node
        const node = new SigleLinkNode(element)

        //  empty
        if (this.isEmpty()) {
            this.rear = node;
            this.rear.next = this.rear
        } else {
            // swap 
            const current = this.rear;
            this.rear = node
            this.rear.next = current.next
            current.next = this.rear;
        }

        this.count++
    }
    getFront() {
        return this.rear.next.value
    }
    getRear() {
        return this.rear.value
    }
    indexOf(element) {
        if (element === void (0) || element === null) {
            return -1
        }
        let index = 0
        let current = this.rear.next;
        if( current.value === element ){
            return index
        }
        while (current !== this.rear && !this.equals(current.value, element)) {
            current = current.next;
            index++
        }
        let resultIndex = index
        if (current === this.rear && !this.equals(current.value, element)) {
            resultIndex = -1
        }
        return resultIndex
    }
    //  list foeward and back new head
    headForward() {
        this.rear = this.rear.next;
        return this.rear.value
    }
    isEmpty(): boolean {
        return this.size() === 0
    }
    size(): number {
        return this.count;
    }
    printf2Arr():Array<any> {
        let linkArr = [];
        let current = this.rear.next;
        while (current !== this.rear) {
            linkArr.push(current.value)
            current = current.next;
        }
        linkArr.push(current.value)
        console.log(linkArr)
        return linkArr

    }
}


const swipperList = new SigleLinkList()
swipperList.push(0)
swipperList.push(1)
swipperList.push(2)
swipperList.push(3)
swipperList.push(4)
swipperList.push(5)
